/**
 * Created by zqk on 2018/5/4.
 */
var currentTaskId = 0;
var projectList;
var taskList;
var projectid = $('#projectid').val();
var selectProjectId = 0;
var selectProjectGrade = 0;
var bt_module_add = $('#bt_module_add').attr('id');
var bt_test = $('#bt_test').attr('id');
var bt_version_add = $('#bt_version_add').attr('id');
var openVid = 0;
var openTid = 0;
var openMid = 0;
var ifExpand = 0;
$(document).ready(function () {
    var form = new xhForm($('#xh_form'));
    taskList = $('#taskList');
    $('.xh-datagrid-toolbar').parseToolbar();

    //设置禁用
    $('#bt_list_edit').setButtonDisable('eu');//任务只有选中才能编辑
    $('#bt_list_link').setButtonDisable('eu');
    $('#bt_list_del').setButtonDisable('eu');//任务只有选中才能删除
    $('#bt_test').hide();
    if (bt_version_add) {
        $('#bt_module_add').hide();
    } else {
        $('#bt_module_add').show();
    }
    $('#pro_bt_list_delete').setButtonDisable('eu');
    $('#pro_bt_list_edit').setButtonDisable('eu');
    $('#pro_bt_list_test').setButtonDisable('eu');
    $('#pro_bt_list_check').setButtonDisable('eu');
    form.parse();
    form.bindRequired($('#bt_form_add,#bt_form_edit'));//绑定事件

    //task
    $btn_menu = $('#bt_list_state').menubutton({
        iconCls: 'fa fa-bars',
        menu: '',
    });

    function progressFormatter(value, rowData, rowIndex) {
        htmlstr = '<div  class="easyui-progressbar progressbar"  style="width: 100px; height: 22px;">'
            + '<div class="progressbar-text" style="width: 100px; height: 20px; line-height: 20px;">' + value + '%</div> '
            + '	<div class="progressbar-value" style="width:' + value + '%; height: 20px; line-height: 20px;">'
            + '<div class="progressbar-text" style="width: 100px; height: 20px; line-height: 20px;">' + value + '%</div>'
            + '</div>';
        return htmlstr;
    }

    taskList.treegrid({
        url: moduleUrl + 'task/getTask?iProjectId=' + projectid,
        method: 'post',
        rownumbers: true,
        pagination: true,
        pageSize: 50,
        pageList: [20,50,100,200],
        idField: 'iTaskId',
        treeField: 'sTaskName',
        singleSelect: true,
        frozenColumns: [[
            {field: 'ck', checkbox: true},
            {
                field: 'ifExpand', title: '', width: 34, formatter: function (value, row, index) {
                    if (value) {
                        return '<div style="text-align:center;height:16px;" class="datagrid-cell ifExpand undefined"><span class="datagrid-row-expander datagrid-row-expand" style="display:inline-block;width:16px;height:16px;cursor:pointer;"></span></div>';
                    } else {
                        return '<div style="text-align:center;height:16px;" class="datagrid-cell ifExpand undefined"><span class="datagrid-row-expander datagrid-row-expand" style="display:none;width:16px;height:16px;cursor:pointer;"></span></div>';
                    }
                }
            },
            {field: 'iTaskId', title: 'iTaskId', hidden: true},
            {field: 'sTaskName', title: '任务名称', width: 240, halign: 'center', align: 'left'},
        ]],
        columns: [[
            {field: 'sTaskContent', title: '任务内容', width: 250, halign: 'center', align: 'left'},
            {field: 'chargeName', title: '负责人', width: 70, halign: 'center', align: 'left'},
            {field: 'sStaffName', title: '执行人', width: 70, halign: 'center', align: 'left'},
            {
                field: 'iLevel', title: '任务等级', width: 80, align: 'center', halign: 'center',
                formatter: function (value, row, index) {
                    if (value == '1') {
                        var str = '<div style = "background-color:#CCCCCC;color: #ffffff;height: 20px; line-height: 20px;">' + taskLevels[value] + '</div>'
                        return str;
                    } else if (value == '2') {
                        var str = '<div style = "background-color:#EEEE00;color: #ffffff;height: 20px; line-height: 20px;">' + taskLevels[value] + '</div>'
                        return str;
                    } else if (value == '3') {
                        var str = '<div style = "background-color:#ff1200;color: #ffffff;height: 20px; line-height: 20px;">' + taskLevels[value] + '</div>'
                        return str;
                    }
                }
            },
            {
                field: 'iTaskStatus', title: '任务状态', width: 80, align: 'center', halign: 'center',
                formatter: function (value, row, index) {
                    if (value == '1') {
                        var str = '<div style = "background-color:#CCCCCC;color: #ffffff;height: 20px; line-height: 20px;">' + taskStatus[value] + '</div>'
                        return str;
                    } else if (value == '2') {
                        var str = '<div style = "background-color:#6495ED;color: #ffffff;height: 20px; line-height: 20px;">' + taskStatus[value] + '</div>'
                        return str;
                    } else if (value == '3') {
                        var str = '<div style = "background-color:#66CDAA;color: #ffffff;height: 20px; line-height: 20px;">' + taskStatus[value] + '</div>'
                        return str;
                    } else if (value == '4') {
                        var str = '<div style = "background-color:#ee0146;color: #ffffff;height: 20px; line-height: 20px;">' + taskStatus[value] + '</div>'
                        return str;
                    }
                }
            },
            {field: 'dTaskTime', title: '任务工时', width: 70, align: 'right', halign: 'center'},
            {field: 'dRealTaskTime', title: '实际工时', width: 70, align: 'right', halign: 'center'},
            {
                field: 'bChecked', title: '是否审核', width: 80, align: 'center', halign: 'center',
                formatter: function (value, row, index) {
                    if (value != 1) {
                        var str = '<div style = "background-color:#ff1b00;color: #ffffff;height: 20px; line-height: 20px;">' + '未审核' + '</div>'
                        return str;
                    } else if (value == 1) {
                        var str = '<div style = "background-color:#0079c8;color: #ffffff;height: 20px; line-height: 20px;">' + '已审核' + '</div>'
                        return str;
                    }
                }
            },
            {
                field: 'iProcess', title: '任务进度', width: 110, halign: 'center',
                formatter: progressFormatter
            },
            {field: 'tFromAt', title: '预计开始时间', width: 125, align: 'center', halign: 'center'},
            {field: 'tEndAt', title: '预计完成时间', width: 125, halign: 'center', align: 'center'},
            {field: 'tStartedAt', title: '实际开始时间', width: 125, halign: 'center', align: 'center'},
            {field: 'tFinishedAt', title: '实际完成时间', width: 125, halign: 'center', align: 'center'},
        ]],
        onSelect: function (row) {
            $btn_menu = $('#bt_list_state').menubutton({
                iconCls: 'fa fa-bars',
                menu: '#changestatus',
            });
            currentTaskId = row['iTaskId'];
            openTid = row['iTaskId'];
            // parentId = taskList.treegrid('getSelections')[0]['iParentId'];
            taskList.treegrid('expand', currentTaskId);
            $('#bt_list_edit').setButtonEnable('eu');
            $('#bt_list_link').setButtonEnable('eu');
            $('#bt_list_del').setButtonEnable('eu');
            $('#bt_tasks_add').setButtonDisable('eu');
            //开启或者完成任务
            $($btn_menu.menubutton('options').menu).menu({
                onClick: function (item) {
                    //item 的相关属性参见API中的menu
                    var taskObj = taskList.treegrid('getSelections')[0];
                    var pid = taskObj['iParentId'];
                    if (pid) {
                        taskObj['_parentId'] = pid;
                        var pTaskObj = taskList.treegrid('getParent', currentTaskId);
                        if (pTaskObj) {
                            if (pTaskObj['iTaskStatus'] == 3 || pTaskObj['iTaskStatus'] == 4) {
                                window.top.xhTop.showError('错误', '父级任务已完成或已关闭');
                                return false;
                            }
                        }
                    }

                    var status;
                    if (item.text == '开始') {
                        status = 2;
                    } else if (item.text == '完成') {
                        status = 3;
                    } else if (item.text == '关闭') {
                        status = 4;
                    }
                    if (taskList.treegrid('getSelections').length == 0) {
                        window.top.xhTop.showError('错误', '请选择一个任务');
                        return false;
                    }
                    var iTaskId = taskObj['iTaskId'];
                    if (status == 3) {
                        window.top.swal({
                            showCancelButton: true,
                            confirmButtonText: '确认',
                            cancelButtonText: '取消',
                            showLoaderOnConfirm: true,
                            allowOutsideClick: false,
                            title: '实际工时',
                            width:400,
                            input: 'text',
                            preConfirm: function(dRealTaskTime) {
                                return new Promise(function (resolve,reject) {
                                    if(dRealTaskTime){
                                        var reg1 = /^\+?(\d*\.\d{1,2})$/;
                                        var reg2 = /^\+?[1-9][0-9]*$/;
                                        if(!reg1.test(dRealTaskTime)&&!reg2.test(dRealTaskTime)){
                                            reject('实际工时格式错误');
                                            return false;
                                        };
                                    }else{
                                        reject('实际工时不能为空');
                                        return false;
                                    }
                                    $.ajax({
                                        url: moduleUrl + 'task/changeStatus',
                                        data: {iTaskId: iTaskId, iTaskStatus: status, iProjectId: projectid,dRealTaskTime: dRealTaskTime},
                                        type: 'POST',
                                        success: function (data) {
                                            window.top.xhTop.showSuccess(
                                                data.msg
                                            );
                                            taskList.treegrid('reload');
                                            projectList.treegrid('reload');
                                        },
                                        error: function (data) {
                                            window.top.xhTop.showError(
                                                data.responseJSON.error,
                                                data.responseJSON.msg
                                            );
                                        }
                                    });
                                });
                            }
                        });
                    }else{
                        $.ajax({
                            url: moduleUrl + 'task/changeStatus',
                            data: {iTaskId: iTaskId, iTaskStatus: status, iProjectId: projectid},
                            type: 'POST',
                            success: function (data) {
                                window.top.xhTop.showSuccess(
                                    data.msg
                                );
                                taskList.treegrid('reload');
                                projectList.treegrid('reload');
                            },
                            error: function (data) {
                                window.top.xhTop.showError(
                                    data.responseJSON.error,
                                    data.responseJSON.msg
                                );
                            }
                        });
                    }

                }
            })
        },
        onBeforeSelect: function (row) {
            if (ifExpand > 0) {
                if (ifExpand == 1) {
                    var datas = row.expandContent;
                    if (datas.length > 0) {
                        var content = '<tr class="expandId' + row.iTaskId + '"><td colspan="5" style="padding-left: 30px;border:0px;"><table style="border-left: 1px dotted lightgrey"><tr><th height="20" style="text-align: center;background-color: #00c898;color: #FFFFFF;" width="200">任务名称</th><th height="20" style="text-align: center;background-color: #00c898;color: #FFFFFF;" width="80" >任务状态</th><th height="20" style="text-align:center;background-color:#00c898;color: #FFFFFF;" width="100">执行人</th></tr>';
                        var content1 = '<tr class="expandId' + row.iTaskId + '"><td colspan="11"><table><tr><th height="20" style="text-align:center;"> </th></tr>';
                        $.each(datas, function (index, data) {
                            content += '<tr><td height="20" style="text-align: center">' + data.sTaskName + '</td><td height="20" style="text-align: center">' + taskStatus[data.iTaskStatus] + '</td><td height="20" style="text-align: center">' + data.sStaffName + '</td></tr>';
                            content1 += '<tr><td height="20" style="text-align: center"> </td></tr>';
                        });
                        content += '</table></td></tr>';
                        content1 += '</table></td></tr>';
                    }
                    $("tr[node-id='" + row.iTaskId + "']").eq(0).after(content);
                    $("tr[node-id='" + row.iTaskId + "']").eq(1).after(content1);
                } else if (ifExpand == 2) {
                    $('.expandId' + row.iTaskId).remove();
                }
                ifExpand = 0;
                return false;
            }
            $btn_menu = $('#bt_list_state').menubutton({
                iconCls: 'fa fa-bars',
                menu: '',
            });
            if (currentTaskId == 0) {
                return true;
            } else {
                if (currentTaskId == row['iTaskId']) {
                    $('#bt_list_edit').setButtonDisable('eu');
                    $('#bt_list_link').setButtonDisable('eu');
                    $('#bt_list_del').setButtonDisable('eu');
                    $('#bt_tasks_add').setButtonEnable('eu');
                    taskList.treegrid('unselect', currentTaskId);
                    taskList.treegrid('collapse', currentTaskId);
                    currentTaskId = 0;
                    openTid = 0;
                    return false;
                }
            }
        },
        onBeforeCheck: function (row) {
            if (ifExpand > 0) {
                if (ifExpand == 1) {
                    var datas = row.expandContent;
                    if (datas.length > 0) {
                        var content = '<tr class="expandId' + row.iTaskId + '"><td colspan="5" style="padding-left: 30px"><table style="border-left: 1px dotted lightgrey"><tr><th height="20" style="text-align: center;background-color: #0079c8;border-width: 1px; 1px 1px 1px;color: #FFFFFF;border-color: #FFFFFF" width="250">任务名称</th><th height="20" style="text-align: center;background-color: #0079c8;border-width: 1px; 1px 1px 1px;color: #FFFFFF;" width="80" >任务状态</th></tr>';
                        var content1 = '<tr class="expandId' + row.iTaskId + '"><td colspan="3"><table><tr><th height="20" style="text-align:center;background-color:#0079c8;border-width: 1px; 1px 1px 1px;color: #FFFFFF;" width="100">执行人</th></tr>';
                        $.each(datas, function (index, data) {
                            content += '<tr><td height="20" style="text-align: center">' + data.sTaskName + '</td><td height="20" style="text-align: center">' + taskStatus[data.iTaskStatus] + '</td></tr>';
                            content1 += '<tr><td height="20" style="text-align: center">' + data.sStaffName + '</td></tr>';
                        });
                        content += '</table></td></tr>';
                        content1 += '</table></td></tr>';
                    }
                    $("tr[node-id='" + row.iTaskId + "']").eq(0).after(content);
                    $("tr[node-id='" + row.iTaskId + "']").eq(1).after(content1);
                } else if (ifExpand == 2) {
                    $('.expandId' + row.iTaskId).remove();
                }
                ifExpand = 0;
                return false;
            }
            $btn_menu = $('#bt_list_state').menubutton({
                iconCls: 'fa fa-bars',
                menu: '',
            });
            if (currentTaskId == 0) {
                return true;
            } else {
                if (currentTaskId == row['iTaskId']) {
                    taskList.treegrid('unselect', currentTaskId);
                    taskList.treegrid('uncheckRow', currentTaskId);
                    currentTaskId = 0;
                    openTid = 0;
                    $('#bt_list_edit').setButtonDisable('eu');
                    $('#bt_list_link').setButtonDisable('eu');
                    $('#bt_list_del').setButtonDisable('eu');
                    $('#bt_tasks_add').setButtonEnable('eu');
                    return false;
                }
            }
        },
        onLoadSuccess: function (row, data) {
            function getParent(pid) {
                var taskObj = taskList.treegrid('find', pid);
                if (taskObj) {
                    taskList.treegrid('expand', pid);
                    if (taskObj['iParentId'] != 0) {
                        taskList.treegrid('expand', taskObj['iParentId']);
                        var parentObj = taskList.treegrid('find', taskObj['iParentId']);
                        if (parentObj) {
                            if (parentObj['iParentId'] != 0) {
                                getParent(parentObj['iParentId']);
                            }
                        }
                    }
                }
            }

            if (openTid) {
                getParent(openTid);
                taskList.treegrid('select', openTid);
            }
            $('.ifExpand').click(function () {
                var className = $(this).children('span').attr('class');
                if (className == 'datagrid-row-expander datagrid-row-expand') {
                    ifExpand = 1;
                    $(this).children('span').attr('class', 'datagrid-row-expander datagrid-row-collapse');
                } else if (className == 'datagrid-row-expander datagrid-row-collapse') {
                    ifExpand = 2;
                    $(this).children('span').attr('class', 'datagrid-row-expander datagrid-row-expand');
                }
            });
        },
        onBeforeLoad: function (row, param) {
            taskList.treegrid('clearSelections');
            currentTaskId = 0;
        },
        onBeforeExpand: function (row) {
            $('.expandId' + row.iTaskId).prev().find('span.datagrid-row-expander').attr('class', 'datagrid-row-expander datagrid-row-expand');
            $('.expandId' + row.iTaskId).remove();
        },
        onBeforeCollapse: function (row) {
            $('.expandId' + row.iTaskId).prev().find('span.datagrid-row-expander').attr('class', 'datagrid-row-expander datagrid-row-expand');
            $('.expandId' + row.iTaskId).remove();
        }

    });


    $('#bt_list_search').click(function () {
        var status = $('#taskstatus').val();
        var level = $('#tasklevel').val();
        var userid = $('#memberid').val();
        taskList.treegrid('load', {
            status: status,
            level: level,
            userid: userid,
            projectid: projectid,
            selectProjectId: selectProjectId
        });
    });

    $('#taskList_toolbar a').click(function () {
        var eleid = $(this).attr('id');
        $.ajax({
            url: moduleUrl + 'task/checkIfNewTask',
            data: {iProjectId: projectid, selectProjectGrade: selectProjectGrade, selectProjectId: selectProjectId},
            type: 'POST',
            success: function (data) {
                var obj = taskList.treegrid('getSelections')[0];
                var projobj = projectList.treegrid('getSelections')[0];
                if (eleid == 'bt_list_add') {
                    if (obj) {
                        if (obj['iTaskStatus'] == 3 || obj['iTaskStatus'] == 4) {
                            window.top.xhTop.showError('错误', '任务已完成或已关闭');
                            return false;
                        }
                    }
                    if (projobj) {
                        if (projobj['iProjectGrade'] == 2) {
                            var title = projectName + ' - ' + projobj['sProjectName'] + ' - 新增任务';
                        } else if (projobj['iProjectGrade'] == 3) {
                            projobj['_parentId'] = projobj['iParentPid'];
                            var parentObj = projectList.treegrid('getParent', selectProjectId);
                            var title = projectName + ' - ' + parentObj['sProjectName'] + ' - ' + projobj['sProjectName'] + ' - 新增任务';
                        }
                    } else {
                        var title = projectName + ' - 新增任务';
                    }
                    if (obj != undefined) {
                        window.top.xhTop.showModal({
                            title: title,
                            content: '<iframe src="' + moduleUrl + 'task/taskForm?iParentId=' + obj['iTaskId'] + '&iProjectId=' + projectid + '&selectProjectGrade=' + selectProjectGrade + '&selectProjectId=' + selectProjectId + '"  style="width: 100%;border-style: none;padding: 0px;height: 480px"  scrolling="yes"></iframe>',
                            button: '提交',
                            backClose: false
                        });
                    } else {
                        window.top.xhTop.showModal({
                            title: title,
                            content: '<iframe src="' + moduleUrl + 'task/taskForm?iProjectId=' + projectid + '&selectProjectGrade=' + selectProjectGrade + '&selectProjectId=' + selectProjectId + ' " style="width: 100%;border-style: none;padding: 0px;height: 480px"  scrolling="yes"></iframe>',
                            button: '提交',
                            backClose: false
                        });
                    }
                } else if (eleid == 'bt_list_edit') {
                    if (!obj) {
                        return false;
                    } else {
                        if (obj['iTaskStatus'] == 3 || obj['iTaskStatus'] == 4) {
                            window.top.xhTop.showError('错误', '任务已完成或已关闭');
                            return false;
                        }
                    }
                    if (projobj) {
                        if (projobj['iProjectGrade'] == 2) {
                            var title = projectName + ' - ' + projobj['sProjectName'] + ' - 编辑任务';
                        } else if (projobj['iProjectGrade'] == 3) {
                            projobj['_parentId'] = projobj['iParentPid'];
                            var parentObj = projectList.treegrid('getParent', selectProjectId);
                            var title = projectName + ' - ' + parentObj['sProjectName'] + ' - ' + projobj['sProjectName'] + ' - 编辑任务';
                        }
                    } else {
                        var title = projectName + ' - 编辑任务';
                    }
                    var id = taskList.treegrid('getSelections')[0]['iTaskId'];
                    window.top.xhTop.showModal({
                        title: title,
                        content: '<iframe src="' + moduleUrl + 'task/taskForm?iTaskId=' + id + '&iProjectId=' + projectid + '&selectProjectGrade=' + selectProjectGrade + '&selectProjectId=' + selectProjectId + '"  style="width: 100%;border-style: none;padding: 0px;height: 400px"  scrolling="yes"   ></iframe>',
                        button: '提交',
                        backClose: false,
                    });
                } else if (eleid == 'bt_tasks_add') {
                    if (taskList.treegrid('getSelections')[0]) {
                        return false;
                    }
                    window.top.xhTop.showModal({
                        title: '批量添加任务',
                        width: 800,
                        size: 'lg',
                        content: '<iframe src="' + moduleUrl + 'task/trackTaskList?iProjectId=' + projectid + '&selectProjectGrade=' + selectProjectGrade + '&selectProjectId=' + selectProjectId + '"  style="width: 100%;;border-style: none;padding: 0px;height: 550px"  scrolling="yes"   ></iframe>',
                        backClose: false
                    });
                } else if (eleid == 'bt_list_del') {
                    var obj = taskList.treegrid('getSelections')[0];
                    if (!obj) return false;
                    myWindow.initPopUp({title: '确认删除', text: '确定要删除该任务吗?'}, function () {
                        $.ajax({
                            url: moduleUrl + 'task/delTaskById',
                            data: {iTaskId: obj['iTaskId']},
                            type: 'POST',
                            success: function (data) {
                                window.top.xhTop.showSuccess(
                                    data.msg
                                );
                                taskList.treegrid('clearSelections');
                                taskList.treegrid('reload');
                                projectList.treegrid('reload');
                            },
                            error: function (data) {
                                window.top.xhTop.showError(
                                    data.responseJSON.error,
                                    data.responseJSON.msg
                                )
                            }
                        })
                    });
                } else if (eleid == 'bt_list_link') {
                    var obj = taskList.treegrid('getSelections')[0];
                    if (!obj) return false;
                    window.top.xhTop.showModal({
                        title: '关联任务',
                        width: 660,
                        content: '<iframe src="' + moduleUrl + 'task/publicTasks?iTaskId=' + obj['iTaskId'] + '"  style="width: 100%;border-style: none;padding: 0px;height: 400px"  scrolling="yes"   ></iframe>',
                        backClose: false
                    });
                }
            },
            error: function (data) {
                window.top.xhTop.showError(
                    data.responseJSON.error,
                    data.responseJSON.msg
                );
            }
        });
    });

    //project
    //--------------------------------------------------华丽分割线-------------------------------------------------------
    projectList = $('#projectList');

    function projectProgressFormatter(value, rowData, rowIndex) {
        htmlstr = '<div  class="easyui-progressbar progressbar"  style="width: 140px; height: 22px;">'
            + '<div class="progressbar-text" style="width: 140px; height: 20px; line-height: 20px;">' + value + '%</div> '
            + '<div class="progressbar-value" style="width:' + value + '%; height: 20px; line-height: 20px;">'
            + '<div class="progressbar-text" style="width: 140px; height: 20px; line-height: 20px;">' + value + '%</div>'
            + '</div>';
        return htmlstr;
    }

    $pro_btn_menu = $('#pro_bt_list_state').menubutton({
        iconCls: 'fa fa-bars',
        menu: '',
    });
    $pro_add_menu = $('#addpro').menubutton({
        iconCls: 'fa fa-bars',
        menu: '#addproject',
    });
    projectList.treegrid({
        url: moduleUrl + 'task/getVisions?iProjectId=' + projectid,
        method: 'post',
        rownumbers: true,
        pagination: true,
        pageSize: 50,
        pageList: [20,50,100,200],
        idField: 'iProjectId',
        treeField: 'sProjectName',
        singleSelect: true,
        columns: [[
            {field: 'ck', checkbox: true},
            {field: 'iProjectId', title: 'iProjectId', hidden: true},
            {field: 'iProjectGrade', title: 'iProjectGrade', hidden: true},
            {field: 'sProjectName', title: '名称', width: 200, align: 'left'},
            {
                field: 'iProcess', title: '项目进度', width: 150,
                formatter: projectProgressFormatter
            },
        ]],
        onSelect: function (row) {
            taskList.treegrid('clearSelections');
            currentTaskId = 0;
            row['_parentId'] = row['iParentPid'];
            selectProjectId = row['iProjectId'];
            if (row['iProjectGrade'] == 2) {
                openVid = row['iProjectId'];
                openMid = 0;
            } else if (row['iProjectGrade'] == 3) {
                var parentObj = projectList.treegrid('getParent', selectProjectId);
                openVid = parentObj['iProjectId'];
                openMid = row['iProjectId'];
            }
            selectProjectGrade = row['iProjectGrade'];
            projectList.treegrid('expand', selectProjectId);
            $('#pro_bt_list_edit').setButtonEnable('eu');//编辑enable
            $('#pro_bt_list_test').setButtonEnable('eu');//编辑enable
            $('#pro_bt_list_check').setButtonEnable('eu');//编辑enable
            $('#pro_bt_list_delete').setButtonEnable('eu');//删除enable
            $('#bt_test').show();//添加版本按钮disable
            $pro_add_menu = $('#addpro').menubutton({
                iconCls: 'fa fa-bars',
                menu: '#addproject',
            });
            if (selectProjectGrade == 2) {//操作版本
                $pro_btn_menu = $('#pro_bt_list_state').menubutton({
                    iconCls: 'fa fa-bars',
                    menu: '#projectstatus',
                });
                if (bt_module_add) {//拥有添加模块的权限
                    $('#bt_module_add').show();
                }
                var title = row['sProjectName'] + ' - 任务列表';
            } else if (selectProjectGrade == 3) { //操作模块
                $('#bt_module_add').hide();//添加版本按钮disable
                var parentObj = projectList.treegrid('getParent', selectProjectId);
                var title = parentObj['sProjectName'] + ' - ' + row['sProjectName'] + ' - 任务列表';
            }
            $('.xh-datagrid .panel-title').html(title);
            taskList.treegrid('load', {selectProjectId: selectProjectId});
            if ($pro_btn_menu.length) {
                $($pro_btn_menu.menubutton('options').menu).menu({
                    onClick: function (item) {
                        //item 的相关属性参见API中的menu
                        var status
                        if (item.text == '开发') {
                            status = 2;
                        } else if (item.text == '测试') {
                            status = 3;
                        } else if (item.text == '发布') {
                            status = 4;
                        } else if (item.text == '完成') {
                            status = 5;
                        } else if (item.text == '暂停') {
                            status = 6;
                        } else if (item.text == '重启') {
                            status = 99;
                        }
                        if (projectList.treegrid('getSelections').length == 0) {
                            window.top.xhTop.showError('错误', '请选择一个项目');
                            return false;
                        }
                        var iProject = projectList.treegrid('getSelections')[0];
                        $.ajax({
                            url: moduleUrl + 'project/changeStatus',
                            data: {iProjectId: iProject['iProjectId'], iProjectStatus: status},
                            type: 'POST',
                            success: function (data) {
                                window.top.xhTop.showSuccess(
                                    data.msg
                                );
                                projectList.treegrid('reload');
                            },
                            error: function (data) {
                                window.top.xhTop.showError(
                                    data.responseJSON.error,
                                    data.responseJSON.msg
                                );
                            }
                        });
                    }
                })
            }
        },
        onBeforeSelect: function (row) {
            if (selectProjectId == 0) {
                return true;
            } else {
                if (selectProjectId == row['iProjectId']) {
                    $pro_add_menu = $('#addpro').menubutton({
                        iconCls: 'fa fa-bars',
                        menu: '#addproject',
                    });
                    $('#bt_version_add').show();
                    $('#bt_module_add').hide();
                    $('#bt_test').hide();
                    projectList.treegrid('unselect', selectProjectId);
                    projectList.treegrid('collapse', selectProjectId);
                    selectProjectId = 0;
                    selectProjectGrade = 0;
                    openVid = 0;
                    openMid = 0;
                    currentTaskId = 0;
                    openTid = 0;
                    taskList.treegrid('clearSelections');
                    $('#bt_tasks_add').setButtonEnable('eu');
                    $('#pro_bt_list_edit').setButtonDisable('eu');
                    $('#pro_bt_list_test').setButtonDisable('eu');
                    $('#pro_bt_list_check').setButtonDisable('eu');
                    $('#pro_bt_list_delete').setButtonDisable('eu');
                    var title = '任务列表';
                    $('.xh-datagrid .panel-title').html(title);
                    taskList.treegrid('load', {selectProjectId: projectid});
                    return false;
                }
            }
        },
        onBeforeCheck: function (row) {
            if (selectProjectId == 0) {
                return true;
            } else {
                if (selectProjectId == row['iProjectId']) {
                    projectList.treegrid('uncheckRow', selectProjectId);
                    projectList.treegrid('collapse', selectProjectId);
                    selectProjectId = 0;
                    selectProjectGrade = 0;
                    openVid = 0;
                    openMid = 0;
                    currentTaskId = 0;
                    openTid = 0;
                    taskList.treegrid('clearSelections');
                    $('#pro_bt_list_edit').setButtonDisable('eu');
                    $('#pro_bt_list_test').setButtonDisable('eu');
                    $('#pro_bt_list_check').setButtonDisable('eu');
                    $pro_add_menu = $('#addpro').menubutton({
                        iconCls: 'fa fa-bars',
                        menu: '#addproject',
                    });
                    $('#bt_version_add').show();
                    $('#bt_module_add').hide();
                    $('#bt_test').hide();
                    $('#bt_tasks_add').setButtonEnable('eu');
                    $('#pro_bt_list_delete').setButtonDisable('eu');
                    var title = '任务列表';
                    $('.xh-datagrid .panel-title').html(title);
                    taskList.treegrid('load', {selectProjectId: projectid});
                    return false;
                }
            }
        },
        onLoadSuccess: function (row, data) {
            if (openVid) {
                if (openMid) {
                    projectList.treegrid('expand', openVid);
                    projectList.treegrid('select', openMid);
                } else {
                    projectList.treegrid('select', openVid);
                }
            }
        },
        onBeforeLoad: function (row, param) {
            projectList.treegrid('clearSelections');
            selectProjectId = 0;
        }
    });


    $('#bt_version_add').click(function () {
        var obj = projectList.treegrid('getSelections')[0];
        var title = projectName + ' - ' + '新增版本';
        window.top.xhTop.showModal({
            title: title,
            content: '<iframe src="' + moduleUrl + 'project/projectForm?iProjectGrade=2&iParentPid=' + projectid + '"  style="width: 100%;;border-style: none;padding: 0px;height: 350px"  scrolling="yes"   ></iframe>',
            button: '提交',
            backClose: false
        });

    });

    $('#bt_module_add').click(function () {
        var obj = projectList.treegrid('getSelections')[0];
        if (obj != undefined) {
            var title = projectName + ' - ' + obj['sProjectName'] + ' - ' + '新增模块';
            window.top.xhTop.showModal({
                title: title,
                content: '<iframe src="' + moduleUrl + 'project/projectForm?iProjectGrade=3&iParentPid=' + obj['iProjectId'] + '"  style="width: 100%;;border-style: none;padding: 0px;height: 350px"  scrolling="yes"   ></iframe>',
                button: '提交',
                backClose: false
            });
        } else {
            return false;
        }
    });

    $('#proList_toolbar a').click(function () {
        var eleid = $(this).attr('id');
        var obj = projectList.treegrid('getSelections')[0];
        if (xhExtra.buttonDisabled($(this))) {
            return false;
        }
        else if (eleid == 'pro_bt_list_delete' && obj != undefined) {
            myWindow.initPopUp({title: '确认删除', text: '确定要删除该项目吗?'}, function () {
                $.ajax({
                    url: moduleUrl + 'project/delProjectById',
                    data: {iProjectId: obj['iProjectId']},
                    type: 'POST',
                    success: function (data) {
                        selectProjectId = 0;
                        selectProjectGrade = 0;
                        window.top.xhTop.showSuccess(
                            data.msg
                        );
                        $pro_add_menu = $('#addpro').menubutton({
                            iconCls: 'fa fa-bars',
                            menu: '#addproject',
                        });
                        $('#bt_version_add').show();
                        $('#bt_module_add').hide();
                        $('#bt_test').hide();
                        projectList.treegrid('clearSelections');
                        projectList.treegrid('reload');
                        taskList.treegrid('load', {projectid: projectid});
                    },
                    error: function (data) {
                        window.top.xhTop.showError(
                            data.responseJSON.error,
                            data.responseJSON.msg
                        );
                    }
                });
            })
        } else if (eleid == 'pro_bt_list_edit' && obj != undefined) {
            if (obj['iProjectGrade'] == 2) {
                var title = '编辑版本';
            } else if (obj['iProjectGrade'] == 3) {
                var title = '编辑模块';
            }
            window.top.xhTop.showModal({
                title: title,
                content: '<iframe src="' + moduleUrl + 'project/projectForm?iProjectId=' + obj['iProjectId'] + '&iProjectGrade=' + obj['iProjectGrade'] + '&iParentPid=' + obj['iParentPid'] + '"  style="width: 100%;border-style: none;padding: 0px;height: 350px"  scrolling="yes"   ></iframe>',
                button: '提交',
                backClose: false,
            });
        } else if (eleid == 'pro_bt_list_check' && obj != undefined) {
            if (obj['iProjectGrade'] == 2) {
                var text = '确认要审核该版本与其模块吗?';
            } else if (obj['iProjectGrade'] == 3) {
                var text = '确认要审核该模块吗?';
            }
            myWindow.initPopUp({title: '审核确认', text: text}, function () {
                $.ajax({
                    url: moduleUrl + 'project/checkProjectById',
                    data: {iProjectId: obj['iProjectId']},
                    type: 'POST',
                    success: function (data) {
                        window.top.xhTop.showSuccess(
                            data.msg
                        );
                        $('#bt_list_edit').setButtonDisable('eu');
                        $('#bt_list_link').setButtonDisable('eu');
                        $('#bt_list_delete').setButtonDisable('eu');
                        $('#bt_list_check').setButtonDisable('eu');
                        $('#bt_list_add').setButtonEnable('eu');
                        projectList.treegrid('reload');
                        taskList.treegrid('reload');
                    },
                    error: function (data) {
                        window.top.xhTop.showError(
                            data.responseJSON.error,
                            data.responseJSON.msg
                        );
                    }
                });
            })
        } else if (eleid == 'pro_bt_list_test' && obj != undefined) {
            var projobj = projectList.treegrid('getSelections')[0];
            if (projobj) {
                if (projobj['iProjectGrade'] == 2) {
                    var title = projectName + ' - ' + projobj['sProjectName'] + ' - 项目测试 ';
                } else if (projobj['iProjectGrade'] == 3) {
                    projobj['_parentId'] = projobj['iParentPid'];
                    var parentObj = projectList.treegrid('getParent', selectProjectId);
                    var title = projectName + ' - ' + parentObj['sProjectName'] + ' - ' + projobj['sProjectName'] + ' - 项目测试';
                }
            } else {
                var title = projectName + ' - 项目测试';
            }
            $.ajax({
                url: moduleUrl + 'test/checkTestListPower',
                data: {iProjectId: obj['iProjectId']},
                type: 'POST',
                success: function (data) {
                    window.top.xhTop.createNewTab(title, moduleUrl + 'test/index?iProjectId=' + obj['iProjectId'], true);
                },
                error: function (data) {
                    window.top.xhTop.showError(
                        data.responseJSON.error,
                        data.responseJSON.msg
                    );
                }
            });
        }
    });
});

